package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.Busdict;
import com.sqx.modules.health.service.IAppPrescriptionService;
import com.sqx.modules.health.service.IBusdictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务字典表 前端控制器
 * </p>
 *
 * @author hsx
 * @since 2022-08-30
 */

@Api(value = "业务字典表", tags = "", description = "")
@RestController
@RequestMapping("/health/app/busdict")
public class BusdictController {
    @Autowired
    private IBusdictService busdictService;
    @Autowired
    private IAppPrescriptionService appPrescriptionService;
    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新业务字典表", notes = "新增或更新业务字典表")
    public Result save(@RequestBody Busdict busdict){
        System.out.println(busdict.toString());
        return Result.success(busdictService.saveOrUpdate(busdict));
    }

    // 根据ID删除
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除业务字典表", notes = "根据ID删除业务字典表")
    public Result delete(@PathVariable Integer id){
        return Result.success(busdictService.removeById(id));
    }

    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除业务字典表", notes = "根据IDS删除业务字典表")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(busdictService.removeByIds(ids));
    }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部业务字典表", notes = "查询全部业务字典表")
    public Result findAll(){
        return Result.success(busdictService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录业务字典表", notes = "根据ID查询一条记录业务字典表")
    public Result findOne(@PathVariable Integer id){
        return Result.success(busdictService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询业务字典表", notes = "分页查询业务字典表")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String searchName){
        QueryWrapper<Busdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchName), "name", searchName);
        //queryWrapper.eq( "status", 1);
        queryWrapper.eq("isdelete", 0);
        queryWrapper.orderByAsc("type");
        return  Result.success(busdictService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }

    // 分页查询
    @GetMapping("/findPage2")
    @ApiOperation(value = "分页查询业务字典表", notes = "分页查询业务字典表")
    public Result findPageDisplayPrescriptName(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String searchName){
        QueryWrapper<Busdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchName), "name", searchName);
        queryWrapper.like(ObjectUtil.isNotEmpty(searchName), "type", searchName);
        queryWrapper.eq( "status", 1);
        queryWrapper.orderByDesc("type");
        queryWrapper.orderByDesc("id");
        Map<String, String> params = new HashMap<>();
        Map<String, String> returnDepart = new HashMap<>();
        params.put("type","department");
        List <Busdict> department = (List<Busdict>) findConditionPage(params).getData();
        department.forEach(one->{
            returnDepart.put(one.getValue(),one.getName());
        });
        return  Result.success(busdictService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    // 条件查询
    @GetMapping("/findConditionPage")
    @ApiOperation(value = "条件查询业务字典表", notes = "条件查询业务字典表")
    public Result findConditionPage(@RequestParam Map<String, String> params){
        QueryWrapper<Busdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(params.get("level")), "level", params.get("level"))
                .in(ObjectUtil.isNotEmpty(params.get("ids")), "id", params.get("ids"))
                .eq(ObjectUtil.isNotEmpty(params.get("type")), "type", params.get("type"))
                .eq(ObjectUtil.isNotEmpty(params.get("pid")), "pid", params.get("pid"))
                .eq("isdelete", 0)
                .eq( "status", 1)
                .like(ObjectUtil.isNotEmpty(params.get("name")), "name", params.get("name"));
        //queryWrapper.orderByDesc("id");
        return  Result.success(busdictService.list(queryWrapper));
    }

    // 条件查询
    @GetMapping("/findConditionArray")
    @ApiOperation(value = "查询业务字典表", notes = "查询业务字典表")
    public Result findConditionArray(@RequestParam Map<String, String> params){
        return  Result.success(busdictService.getViewList(params));
    }
    // 获取检查类型
    @GetMapping("/findCheckType")
    @ApiOperation(value = "获取检查类型", notes = "获取检查类型")
    public Result findCheckType(@RequestParam String type){
        QueryWrapper<Busdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(type),"type", type);
        queryWrapper.eq("isdelete", 0);
        queryWrapper.orderByAsc("type");
        return Result.success(busdictService.list(queryWrapper));
    }
    // 分页查询
    @GetMapping("/findPrecriptPagePage")
    @ApiOperation(value = "查询药方配置信息", notes = "查询药方配置信息字典表")
    public Result findPrecriptPagePage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String searchName){
        QueryWrapper<Busdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchName), "name", searchName);
        //queryWrapper.eq( "status", 1);
        queryWrapper.eq("isdelete", 0);
        queryWrapper.eq("status", 1);
        queryWrapper.ne("level", 99);
        queryWrapper.orderByAsc("type");
        return  Result.success(busdictService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
}

